<template>
	<view class="box">
		<template v-if="list.length>0">
			<view class="hbh_up" v-for="(item,index) in list" :key="index">
				<view class="leftImage">
					<image :src="item.pictureBase64" mode="scaleToFill"></image>
					<view class="btn" @click="addList(item,index)">
						<image class="btn"
							:src="item.isdown?'/static/image/addlisted.png':'/static/image/addlist.png'"
							mode=""></image>
					</view>
				</view>
				<view class="rightTxt">
					<view class="top">
						{{item.descrie?item.descrie:'无'}}
					</view>
					<view class="end">
						<view class="novelName">
							{{item.bookInfo?item.bookInfo.split("-hbh-")[0]:'异常'}}
						</view>
						<view class="tip">
							<text>作者：{{item.bookInfo?item.bookInfo.split("-hbh-")[1]:'异常'}}</text>
							<text>字数：{{item.bookInfo?item.bookInfo.split("-hbh-")[2]:'异常'}}</text>
						</view>
						<view class="assist">
							<view class="rightBox">
								<image :src="item.isdown?'/static/image/downloaded.png':'/static/image/download.png'" mode="" @click="addList(item,index)"></image>
								<text :style="{color:item.isdown?'#ccb868':'#8a8a8a'}" @click="addList(item,index)">{{item.isdown?'已加入':'先下载'}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</template>
		<template v-else>
			<view class="loadImage">
				<image src="/static/loading.webp" mode=""></image>
			</view>
		</template>
	</view>
</template>

<script>
	import {
		selectTxtNovel
	} from "@/api/index.js"
	import {
		mapMutations,
		mapState
	} from "vuex"
	import fileApi from "@/uni_modules/hbh-fileIo/js_sdk/index.js"
	export default {
		name: "txtList",
		data() {
			return {
				list: [],
				pageSize: 20,
				pageNum: 1
			};
		},
		computed: {
			...mapState("home", ["downTxtLoad", "homeIsRush"]),
		},
		created() {
			// console.log("这里", this.downTxtLoad)
		},
		mounted() {
			
			this.getTxtNovel()
		},
		watch: {
			homeIsRush: {
				handler(newVal, oldVal) {
					this.list = []
					this.getTxtNovel()
				}
			}
		},
		methods: {
			...mapMutations(["home/addDownLoad", "home/saveJpgTOtxt", "home/rushHomeTxtBox","home/saveDescribeTOtxt"]),
			getTxtNovel() {
				selectTxtNovel({
					pageSize: this.pageSize,
					pageNum: this.pageNum
				}).then((res) => {
					console.log(res)
					if (res.code == 200) {
						res.data.forEach(async (item, index) => {
							item.isdown = false//标记小说是否在下载队列
							item.isDecompression=false//标记小说下载完后是否解压分割了
							//检查本地小说与接口数组相同的数据并修改其对应状态
							const b = await fileApi.fileIsExist(
								`downloadTxt/${item.fileName}/${item.bookInfo}`
							)
							if (b) {
								item.isdown = true
							}
						})
						//检查本地存在的修改结果状态
						this.list = res.data
					}
				}).catch((err) => {
					console.log(err)
				})
			},
			handleTxt(item) {
				uni.navigateTo({
					url: `/pages/txtNovelContent/txtNovelContent?bookName= ${item.bookInfo.split("-hbh-")[0]}`
				})
			},
			async addList(item, index) {
				console.log(item)
				if (!item.isdown) {
					item.isdown = true
					this.$set(this.list, index, item)
					this["home/saveJpgTOtxt"](item)
					this["home/saveDescribeTOtxt"](item)
					this["home/addDownLoad"](this.downTxtLoad.concat([{
						fileName: item.fileName,
						bookInfo: item.bookInfo,
						isStart: false,
						isDownSuccess: false,
						totalSize: 0,
						downingSize: 0,
						process: 0
					}]))
				} else {
					uni.showToast({
						icon: "none",
						title: "已添加，下载完成后到离线书架阅读！"
					})
				}

			},
			toNovelStore(){
				uni.navigateTo({
					url:"/pages/searchBook/searchBook"
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		width: 100%;
		display: flex;
		flex-direction: column;
		height: 100%;
		overflow-x: hidden;
		overflow-y: scroll;
		.loadImage {
			width: 100%;
			height:100%;
			display: flex;
			justify-content: center;
			align-items: center;

			image {
				width: 100rpx;
				height: 100rpx;
			}
		}

	}

	.hbh_up {
		margin-bottom: 30rpx;
		height: 300rpx;
		width: 100%;
		display: flex;

		.leftImage {
			width: 225rpx;
			height: 300rpx;
			border-radius: 10rpx;
			overflow: hidden;
			position: relative;

			.btn {
				position: absolute;
				top: 0;
				right: 0;
				width: 36rpx;
				height: 36rpx;
				background-color: #fff;
				border-radius: 50%;

				image {
					width: 100%;
					height: 100%;
				}
			}

			image {
				width: 100%;
				height: 100%;
			}
		}

		.rightTxt {
			width: calc(100% - 225rpx);
			height: 100%;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			padding-left: 20rpx;
			box-sizing: border-box;

			.top {
				// height: 190rpx;
				font-size: 31rpx;
				line-height: 31rpx;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 5;
				/* 显示行数 */
				overflow: hidden;
				text-overflow: ellipsis;
				color: #ccc;
			}

			.end {
				height: 150rpx;

				.novelName {
					height: 50rpx;
					line-height: 50rpx;
					color: #00aa7f;
					font-size: 26rpx;
					display: flex;
					flex-wrap: nowrap;
					overflow: hidden;
				}

				.tip {
					height: 50rpx;
					font-size: 24rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					color: #fff;
				}

				.assist {
					height: 50rpx;
					display: flex;
					justify-content: flex-end;
				
					.rightBox {
						height: 100%;
						display: flex;
						align-items: center;
				
						image {
							width: 36rpx;
							height: 36rpx;
						}
						text{
							font-size: 30rpx;
						}
					}
				}
			}
		}
	}
</style>